package com.ktjy.rabbitmqconsumer7002;

import com.alibaba.fastjson2.JSON;
import com.ktjy.pojo.LogDTO;
import com.ktjy.utils.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class FileLogConsumer {
    public static void main(String[] args) {
        try {
            // 通过 RabbitMQUtil 工具类获取连接
            Connection connection = RabbitMQUtil.newConnection();
            Channel channel = connection.createChannel();
            // 修改队列名为 file_log_queue
            String queueName = "file_log_queue";
            channel.queueDeclare(queueName, true, false, false, null);


            // 监听队列
            channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
                /** 重写 handleDelivery 方法，处理消息 */
                @Override
                public void handleDelivery(String consumerTag,
                                           Envelope envelope, AMQP.BasicProperties properties,
                                           byte[] body) throws IOException {
                    String json = new String(body);
                    LogDTO logDTO = JSON.parseObject(json, LogDTO.class);
                    System.out.println("正在将【" + logDTO.getLevel() +
                            "】级别日志消息写入文件，日志内容：" + logDTO.getContent());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
